from application.dependency.TokenDependency import verify_token, get_current_user
from application.entity.UserEntity import UserAdd, UserOut, UserLogin, UserEdit
from application.model.UserModel import UserModel
from application.util.ResponseUtil import ResponseUtil
from fastapi.responses import JSONResponse
from application.logic import UserLogic
from fastapi import APIRouter, Depends, Body
from typing import Annotated

# 设置路由
router: APIRouter = APIRouter(prefix="/user", tags=["用户"])


@router.get("/all", response_model=UserOut, summary="获取所有用户信息")
async def get_all_data(page: int = 1, page_size: int = 10) -> JSONResponse:
    return ResponseUtil(data=await UserLogic.get_all_data(page=page, page_size=page_size)).success()


@router.post("/register", summary="注册用户")
async def register(user_add: UserAdd) -> JSONResponse:
    await UserLogic.register(user_add=user_add)
    return ResponseUtil().success()


@router.post("/login", summary="登录用户")
async def login(user_login: UserLogin) -> JSONResponse:
    token: str = await UserLogic.login(user_login=user_login)
    return ResponseUtil(data=token).success()


@router.post("/logout", summary="退出登录")
def logout(user_id: int = Depends(verify_token)) -> JSONResponse:
    UserLogic.clear_token(user_id=user_id)
    return ResponseUtil().success()


@router.get("/info/{user_id:int}", summary="根据ID获取用户信息", dependencies=[Depends(verify_token)])
async def get_user_by_id(user_id: int) -> JSONResponse:
    return ResponseUtil(data=await UserLogic.get_user_by_id(user_id=user_id)).success()


@router.post("/edit", summary="修改用户基本信息")
async def edit_user_info(user_edit: UserEdit, user_model: UserModel = Depends(get_current_user)) -> JSONResponse:
    await UserLogic.edit_user_info(user_model=user_model, user_edit=user_edit)
    return ResponseUtil().success()


@router.post("/edit_avatar", summary="修改用户头像")
async def edit_avatar(avatar: Annotated[str, Body(embed=True)],
                      user_model: UserModel = Depends(get_current_user)) -> JSONResponse:
    await UserLogic.edit_avatar(user_model=user_model, avatar=avatar)
    return ResponseUtil().success()


@router.post("/edit_email", summary="修改用户邮箱")
async def edit_email(email: Annotated[str, Body(embed=True)],
                     captcha: Annotated[str, Body(embed=True, max_length=6, min_length=6)],
                     user_model: UserModel = Depends(get_current_user)) -> JSONResponse:
    await UserLogic.edit_email(user_model=user_model, email=email, captcha=captcha)
    return ResponseUtil().success()


@router.post("/edit_phone", summary="修改用户手机号")
async def edit_phone(phone: Annotated[int, Body(embed=True)],
                     user_model: UserModel = Depends(get_current_user)) -> JSONResponse:
    await UserLogic.edit_phone(user_model=user_model, phone=phone)
    return ResponseUtil().success()


@router.post("/edit_password", summary="修改用户密码")
async def edit_password(old_password: Annotated[str, Body(embed=True, min_length=6)],
                        new_password: Annotated[str, Body(embed=True, min_length=6)],
                        user_model: UserModel = Depends(get_current_user)) -> JSONResponse:
    await UserLogic.edit_password(user_model=user_model, old_password=old_password, new_password=new_password)
    return ResponseUtil().success()
